// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import com.nokia.symbian 1.1
import Qt 4.7
import com.nokia.extras 1.1
import "../Script.js" as Script

Page {
    id: choix_page
    property string title : "Choix"

    function loading(){
        statusbar.loading();
    }
    function loadingFinish(){
        statusbar.finished();
    }

    PropertyAnimation {
        id: anim
        targets: [button_bp_ext,button_bp_int]

//        targets: [button_bp_ext,button_bp_int,button_nord_est,button_nord_ouest,button_sud_est,button_sud_ouest]
        properties: "opacity"
        duration: 500
    }
    function choix_lieu(lieu){
        switch(lieu){
            case 1: //bp int
                 m_trafic.declaration(5);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php");
                break;
            case 2: //bp ext
                m_trafic.declaration(0);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php");
                break;
 /*--------------- Nord Ouest ------------------------*/
            case 3:
                m_trafic.declaration(1);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INO");
                break;
            case 4:
                m_trafic.declaration(10);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INO");
                break;
            case 5:
                m_trafic.declaration(11);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INO");
                break;
            case 6:
                m_trafic.declaration(12);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INO");
                break;
/*----------------- Nord Ouest ---------------------------*/
            case 7:
                m_trafic.declaration(2);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INE");
                break;
            case 8:
                m_trafic.declaration(7);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INE");
                break;
            case 9:
                m_trafic.declaration(8);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INE");
                break;
            case 10:
                m_trafic.declaration(9);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?INE");
                break;
/*------------------ Sud Ouest ---------------------*/
            case 11:
                m_trafic.declaration(3);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISO");
                break;
            case 12:
                m_trafic.declaration(13);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISO");
                break;
            case 13:
                m_trafic.declaration(14);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISO");
                break;
            case 14:
                m_trafic.declaration(15);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISO");
                break;
/* ---------------- Sud Est ---------------*/
            case 15:
                m_trafic.declaration(6);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISE");
                break;
            case 16:
                m_trafic.declaration(16);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISE");
                break;
            case 17:
                m_trafic.declaration(17);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISE");
                break;
            case 18:
                m_trafic.declaration(18);
                m_trafic.requete("http://www.infotrafic.com/route.php?region=IDF&link=tpsparc.php?ISE");
                break;
 /*-----------------------------------------*/
            default:
                console.log("Erreur");
                break;
        }
    }
    function choix_direction(lieu,direction){
        if(lieu === 1){ //Nord ouest
                switch(direction){
                case 0: //Direction paris
                    choix_lieu(3);
                    break;
                case 1: //Direction Province
                    choix_lieu(4);
                    break;
                case 2: //Direction ext
                    choix_lieu(5);
                    break;
                case 3: //Direction int
                    choix_lieu(6);
                    break;
                default:
                    console.log("Erreur numero"+ lieu + " Direct = " + direction);
                    break;
                }
        }
        else if(lieu === 2){ //Nord est
            switch(direction){
            case 0: //Direction paris
                choix_lieu(7);
                break;
            case 1: //Direction Province
                choix_lieu(8);
                break;
            case 2: //Direction ext
                choix_lieu(9);
                break;
            case 3: //Direction int
                choix_lieu(10);
                break;
            default:
                console.log("Erreur Nord EST");
                break;
            }
        }
        else if(lieu === 3) {//sud ouest
            switch(direction){
            case 0: //Direction paris
                choix_lieu(11);
                break;
            case 1: //Direction Province
                choix_lieu(12);
                break;
            case 2: //Direction ext
                choix_lieu(13);
                break;
            case 3: //Direction int
                choix_lieu(14);
                break;
            default:
                console.log("Erreur so");
                break;
            }
        }
        else if(lieu === 4){// sud ouest
            switch(direction){
            case 0: //Direction paris
                choix_lieu(15);
                break;
            case 1: //Direction Province
                choix_lieu(16);
                break;
            case 2: //Direction ext
                choix_lieu(17);
                break;
            case 3: //Direction int
                choix_lieu(18);
                break;
            default:
                console.log("Erreur se");
                break;
            }

        }
        else{
            console.log("Erreur de lieu")
        }

    }
    InfoBanner{
        id: banner;
    }

    Connections{
        target: m_trafic
        ignoreUnknownSignals: true
        onSignal_charg: {
            console.debug("Chargement finish signal OK")
            loadingFinish();
            pageStack.push(Qt.createComponent("Temps.qml"));
        }
        onErreur:{
//            desactive_tout(2);
//            messagebox.open();
//            console.debug("erreur de connexion");
            banner.text = "Aucune connexion"
            banner.timeout = 0
            banner.interactive = true
            banner.open()

        }
    }

    tools: ToolBarLayout {
            id: toolBarLayout
            ToolButton {
                   iconSource: "toolbar-back"
                   onClicked: {
                       pageStack.pop();
                   }
               }
        }
    Flickable {
            id : flickable
            anchors.fill: parent
            boundsBehavior: Flickable.DragOverBounds
            contentHeight: 300 * 2

            Button {
                id: button_bp_int
                x: 26
                height: 80
                text: "BP Interieur"
                anchors.top: button_bp_ext.bottom
                anchors.topMargin: 50
                anchors.right: parent.right
                anchors.rightMargin: 14
                anchors.left: parent.left
                anchors.leftMargin: 26
                transformOrigin: Item.Center
                clip: false
                onClicked: {
                    choix_lieu(1)
                    loading()
                }
            }

            Button {
                id: button_bp_ext
                x: 26
                y: 186
                height: 80
                text: "BP Exterieur"
                anchors.right: parent.right
                anchors.rightMargin: 14
                anchors.left: parent.left
                anchors.leftMargin: 26
                anchors.top: button_nord_ouest.bottom
                anchors.topMargin: 50
                onClicked: {
                    choix_lieu(2)
                    loading()
                }
            }


            Button {
                id: button_sud_est
                x: 250
                y: 432
                width: 150
                height: 100
                text: "Sud EST"
                anchors.right: parent.right
                anchors.rightMargin: 20
                onClicked: {
                    Script.sens = 4;
                    selection.open()
                }
            }
            Button {
                id: button_sud_ouest
                y: 432
                width: 150
                height: 100
                text: "Sud OUEST"
                anchors.left: parent.left
                anchors.leftMargin: 20
                onClicked: {
                    Script.sens = 3;
                    selection.open()
                }
            }
            Button {
                id: button_nord_est
                x: 250
                y: 22
                width: 150
                height: 100
                text: "Nord EST"
                anchors.right: parent.right
                anchors.rightMargin: 20
                onClicked: {
                    Script.sens = 2;
                    selection.open()
                }
            }
            Button {
                id: button_nord_ouest
                y: 22
                width: 150
                height: 100
                text: "Nord OUEST"
                anchors.left: parent.left
                anchors.leftMargin: 20
                onClicked: {
                    Script.sens = 1;
                    selection.open()
                }
            }
            SelectionDialog {
                 id: selection
                 titleText: "Choisissez votre direction"
//                 selectedIndex: Script.sens

                 model: ListModel {
                     ListElement { name: "Direction PARIS " }
                     ListElement { name: "Direction PROVINCE " }
                     ListElement { name: "Rocades Sens EXTERIEUR" }
                     ListElement { name: "Rocades Sens INTERIEUR" }
                 }
                 onSelectedIndexChanged: {
                     loading()
                     console.debug("Selection = " + selectedIndex)
                     choix_direction(Script.sens,selection.selectedIndex)
                     selectedIndex = -1
                 }
             }


    }
}
